/*
 * @Description: 设备管理相关API
 * @Author: charles
 * @Date: 2023-06-10 10:00:00
 * @LastEditors: charles
 * @LastEditTime: 2023-06-10 10:00:00
 */
import { get, post, put, del } from '@/utils/request'

// 本地模拟设备数据
const mockDeviceList = [
  {
    id: '1001',
    serial_number: 'DEV-20230001',
    name: '监控设备A-01',
    online_status: 1,
    bind_status: 1,
    location: '一号楼监控室',
    ip_address: '192.168.1.101',
    mac_address: '00:1B:44:11:3A:B7',
    create_time: '2023-01-15 08:30:45',
    last_online_time: '2023-06-10 09:15:23'
  },
  {
    id: '1002',
    serial_number: 'DEV-20230002',
    name: '温湿度传感器B-01',
    online_status: 1,
    bind_status: 1,
    location: '数据中心机房',
    ip_address: '192.168.1.102',
    mac_address: '00:1B:44:11:3A:C8',
    create_time: '2023-01-18 10:22:30',
    last_online_time: '2023-06-10 09:12:05'
  },
  {
    id: '1003',
    serial_number: 'DEV-20230003',
    name: '门禁系统C-01',
    online_status: 0,
    bind_status: 1,
    location: '主楼大门',
    ip_address: '192.168.1.103',
    mac_address: '00:1B:44:11:3B:D9',
    create_time: '2023-02-05 14:45:12',
    last_online_time: '2023-06-09 22:30:18'
  },
  {
    id: '1004',
    serial_number: 'DEV-20230004',
    name: '智能照明D-01',
    online_status: 1,
    bind_status: 0,
    location: '二号楼走廊',
    ip_address: '192.168.1.104',
    mac_address: '00:1B:44:11:3C:E5',
    create_time: '2023-02-10 16:33:27',
    last_online_time: '2023-06-10 09:10:55'
  },
  {
    id: '1005',
    serial_number: 'DEV-20230005',
    name: '能耗监测仪E-01',
    online_status: 0,
    bind_status: 0,
    location: '配电室',
    ip_address: '192.168.1.105',
    mac_address: '00:1B:44:11:3D:F6',
    create_time: '2023-02-15 09:18:40',
    last_online_time: '2023-06-08 18:45:32'
  },
  {
    id: '1006',
    serial_number: 'DEV-20230006',
    name: '智能空调F-01',
    online_status: 1,
    bind_status: 1,
    location: '会议室',
    ip_address: '192.168.1.106',
    mac_address: '00:1B:44:11:3E:G7',
    create_time: '2023-03-01 11:25:33',
    last_online_time: '2023-06-10 09:05:12'
  },
  {
    id: '1007',
    serial_number: 'DEV-20230007',
    name: '安防摄像头G-01',
    online_status: 1,
    bind_status: 1,
    location: '停车场',
    ip_address: '192.168.1.107',
    mac_address: '00:1B:44:11:3F:H8',
    create_time: '2023-03-05 13:42:19',
    last_online_time: '2023-06-10 09:08:47'
  },
  {
    id: '1008',
    serial_number: 'DEV-20230008',
    name: '考勤机H-01',
    online_status: 0,
    bind_status: 1,
    location: '人事部门',
    ip_address: '192.168.1.108',
    mac_address: '00:1B:44:11:4A:I9',
    create_time: '2023-03-10 08:50:55',
    last_online_time: '2023-06-09 17:30:22'
  },
  {
    id: '1009',
    serial_number: 'DEV-20230009',
    name: '信息发布屏I-01',
    online_status: 1,
    bind_status: 0,
    location: '大厅',
    ip_address: '192.168.1.109',
    mac_address: '00:1B:44:11:4B:J0',
    create_time: '2023-03-15 15:28:36',
    last_online_time: '2023-06-10 09:01:33'
  },
  {
    id: '1010',
    serial_number: 'DEV-20230010',
    name: '气体检测仪J-01',
    online_status: 1,
    bind_status: 1,
    location: '实验室',
    ip_address: '192.168.1.110',
    mac_address: '00:1B:44:11:4C:K1',
    create_time: '2023-03-20 10:15:48',
    last_online_time: '2023-06-10 08:55:19'
  }
];

// 功能标记 - 控制是否使用模拟数据
const USE_MOCK_DATA = true;

// 获取设备分页列表
export function getDeviceList(params) {
  if (USE_MOCK_DATA) {
    return new Promise((resolve) => {
      console.log('使用模拟设备数据', params);
      setTimeout(() => {
        // 过滤数据
        let filteredList = [...mockDeviceList];
        
        // 按设备名称搜索
        if (params.name) {
          const keyword = params.name.toLowerCase();
          filteredList = filteredList.filter(item => 
            item.name.toLowerCase().includes(keyword) || 
            item.serial_number.toLowerCase().includes(keyword)
          );
        }
        
        // 按在线状态过滤
        if (params.online_status !== undefined && params.online_status !== null && params.online_status !== '') {
          filteredList = filteredList.filter(item => item.online_status === params.online_status);
        }
        
        // 按绑定状态过滤
        if (params.bind_status !== undefined && params.bind_status !== null && params.bind_status !== '') {
          filteredList = filteredList.filter(item => item.bind_status === params.bind_status);
        }
        
        // 计算总数
        const total = filteredList.length;
        
        // 分页处理
        const page = params.page || 1;
        const pageSize = params.pageSize || 10;
        const start = (page - 1) * pageSize;
        const end = start + pageSize;
        const list = filteredList.slice(start, end);
        
        resolve({
          status: 200,
          message: '获取成功',
          data: {
            list,
            total,
            page,
            pageSize
          }
        });
      }, 500);
    });
  }
  
  return get('/device/pageQuery', params);
}

// 获取设备详情
export function getDeviceDetail(id) {
  if (USE_MOCK_DATA) {
    return new Promise((resolve) => {
      console.log('获取模拟设备详情', id);
      setTimeout(() => {
        const device = mockDeviceList.find(item => item.id === id);
        
        if (device) {
          resolve({
            status: 200,
            message: '获取成功',
            data: device
          });
        } else {
          resolve({
            status: 404,
            message: '设备不存在',
            data: null
          });
        }
      }, 300);
    });
  }
  
  return get(`/device/detail?id=${id}`);
}

// 添加设备
export function addDevice(data) {
  if (USE_MOCK_DATA) {
    return new Promise((resolve) => {
      console.log('添加模拟设备', data);
      setTimeout(() => {
        const newId = `${1011 + mockDeviceList.length}`;
        const newDevice = {
          id: newId,
          serial_number: `DEV-${new Date().getFullYear()}${String(mockDeviceList.length + 1).padStart(4, '0')}`,
          create_time: new Date().toLocaleString(),
          last_online_time: data.online_status === 1 ? new Date().toLocaleString() : null,
          ...data
        };
        
        mockDeviceList.unshift(newDevice);
        
        resolve({
          status: 200,
          message: '添加成功',
          data: newDevice
        });
      }, 500);
    });
  }
  
  return post('/device/add', data);
}

// 更新设备
export function updateDevice(data) {
  if (USE_MOCK_DATA) {
    return new Promise((resolve) => {
      console.log('更新模拟设备', data);
      setTimeout(() => {
        const index = mockDeviceList.findIndex(item => item.id === data.id);
        
        if (index !== -1) {
          // 更新设备信息
          mockDeviceList[index] = {
            ...mockDeviceList[index],
            ...data,
            // 如果在线状态改变为在线，更新最后在线时间
            last_online_time: data.online_status === 1 ? new Date().toLocaleString() : mockDeviceList[index].last_online_time
          };
          
          resolve({
            status: 200,
            message: '更新成功',
            data: mockDeviceList[index]
          });
        } else {
          resolve({
            status: 404,
            message: '设备不存在',
            data: null
          });
        }
      }, 500);
    });
  }
  
  return put('/device/update', data);
}

// 删除设备
export function deleteDevice(id) {
  if (USE_MOCK_DATA) {
    return new Promise((resolve) => {
      console.log('删除模拟设备', id);
      setTimeout(() => {
        const index = mockDeviceList.findIndex(item => item.id === id);
        
        if (index !== -1) {
          mockDeviceList.splice(index, 1);
          resolve({
            status: 200,
            message: '删除成功'
          });
        } else {
          resolve({
            status: 404,
            message: '设备不存在'
          });
        }
      }, 300);
    });
  }
  
  return del(`/device/delete?id=${id}`);
}

// 切换设备在线状态（模拟用）
export function toggleDeviceStatus(id, status) {
  if (USE_MOCK_DATA) {
    return new Promise((resolve) => {
      console.log(`切换模拟设备状态：${id}, 状态: ${status}`);
      setTimeout(() => {
        const index = mockDeviceList.findIndex(item => item.id === id);
        
        if (index !== -1) {
          mockDeviceList[index].online_status = status;
          // 如果在线，更新最后在线时间
          if (status === 1) {
            mockDeviceList[index].last_online_time = new Date().toLocaleString();
          }
          
          resolve({
            status: 200,
            message: status === 1 ? '设备已上线' : '设备已下线',
            data: mockDeviceList[index]
          });
        } else {
          resolve({
            status: 404,
            message: '设备不存在',
            data: null
          });
        }
      }, 300);
    });
  }
  
  return put('/device/updateStatus', { id, status });
}

// 切换设备绑定状态
export function toggleDeviceBindStatus(id, status) {
  if (USE_MOCK_DATA) {
    return new Promise((resolve) => {
      console.log(`切换设备绑定状态：${id}, 状态: ${status}`);
      setTimeout(() => {
        const index = mockDeviceList.findIndex(item => item.id === id);
        
        if (index !== -1) {
          mockDeviceList[index].bind_status = status;
          
          resolve({
            status: 200,
            message: status === 1 ? '设备已绑定' : '设备已解绑',
            data: mockDeviceList[index]
          });
        } else {
          resolve({
            status: 404,
            message: '设备不存在',
            data: null
          });
        }
      }, 300);
    });
  }
  
  return put('/device/updateBindStatus', { id, status });
} 